import { AxiosRequestConfig } from 'axios'
import service from './request'

/**
 * 上传图片文件
 * @param url 上传接口地址
 * @param file 图片文件
 * @param extraData 额外的表单数据（可选）
 * @returns Promise<T>
 */
export const uploadImage = <T = any>({
  url,
  file,
  extraData
}: {
  url: string
  file: File
  extraData?: Record<string, any>
}): Promise<T> => {
  const formData = new FormData()
  formData.append('file', file)

  if (extraData) {
    Object.entries(extraData).forEach(([key, value]) => {
      formData.append(key, value)
    })
  }

  return service({
    url,
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  } as AxiosRequestConfig)
}
